package com.haopt.clock.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haopt.clock.dao.TApplicationMapper;
import com.haopt.clock.dao.TUserMapper;
import com.haopt.clock.dto.ResultDTO;
import com.haopt.clock.entity.TApplication;
import com.haopt.clock.entity.TUser;
import com.haopt.clock.service.ApplicationService;
import com.haopt.clock.utils.DataUtil;
import com.haopt.clock.utils.IdBuilderUtil;
import com.haopt.clock.vo.ReqApplicationPageVO;
import com.haopt.clock.vo.ResApplicationDetailVO;
import com.haopt.clock.vo.ResIdVO;
import com.haopt.clock.vo.ResPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author haopt
 * @version 1.0
 * @ClassName ApplicationServiceImpl
 * @Description TODO
 * @date 2021/3/22 15:35
 */
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private TUserMapper userMapper;
    @Autowired
    private TApplicationMapper applicationMapper;

    @Override
    public ResultDTO<ResIdVO> application(String id) {
        //判断当前用户是否存在
        TUser user = userMapper.selectByPrimaryKey(id);
        if(user == null){
            return ResultDTO.errorOf(1002, "当前用户信息不存在，请重新登录");
        }
        if(user.getRole() == 2){
            return ResultDTO.errorOf(1002, "用户处于管理员状态，不能进行重复申请");
        }
        //判断当前用户是否申请
        TApplication searBean = new TApplication();
        searBean.setDeleted(1);
        searBean.setState(1);
        searBean.setUserId(id);
        int count = applicationMapper.selectCount(searBean);
        if(count > 0){
            return ResultDTO.errorOf(1004, "您已经申请成为管理员，请等待审核");
        }
        //添加申请
        TApplication application = new TApplication();
        application.setId(IdBuilderUtil.getId());
        application.setCreateTime(System.currentTimeMillis());
        application.setUpdateTime(System.currentTimeMillis());
        application.setState(1);
        application.setDeleted(1);
        application.setUserId(id);
        applicationMapper.insertSelective(application);
        return ResultDTO.okOf(new ResIdVO(application.getId()));
    }

    @Override
    public ResultDTO<ResPageVO<ResApplicationDetailVO>> listOfPage(ReqApplicationPageVO vo) {
        PageHelper.startPage(vo.getCurrentPage(), vo.getPageSize());
        //判断当前用户是否申请
        List<ResApplicationDetailVO> list = applicationMapper.listByState(vo.getstate());
        PageInfo<ResApplicationDetailVO> info = new PageInfo<>(list);
        if(!CollectionUtils.isEmpty(info.getList())){
            info.getList().forEach(bean->{
                TUser user = userMapper.selectByPrimaryKey(bean.getUserId());
                bean.setUserName(user.getName());
            });
        }
        ResPageVO<ResApplicationDetailVO> res = new ResPageVO<>();
        res.setCurrentPage(info.getPageNum());
        res.setPageSize(info.getSize());
        res.setTotal(info.getTotal());
        res.setList(info.getList());
        return ResultDTO.okOf(res);
    }

    @Override
    public ResultDTO review(String id,Integer state) {
        TApplication bean = applicationMapper.selectByPrimaryKey(id);
        TUser user = userMapper.selectByPrimaryKey(bean.getUserId());
        if(DataUtil.isNullOrEmpty(user)){
            applicationMapper.deleteByPrimaryKey(bean.getId());
            return ResultDTO.errorOf(2001, "用户不存在，该申请被删除");
        }
        //将申请更改
        TApplication application = new TApplication();
        application.setState(state);
        application.setId(id);
        application.setCreateTime(System.currentTimeMillis());
        applicationMapper.updateByPrimaryKeySelective(application);
        //修改用户角色
        if(3 == state){
            user.setRole(2);
            userMapper.updateByPrimaryKeySelective(user);
        }
        return ResultDTO.okOf();
    }
}
